/*
 * 文件名：SupplierService.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：wuliquan
 * 修改时间：2013-2-21
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.consumables.supplier.transaction.impl;

import org.springframework.beans.BeanUtils;

import com.wootide.cms.common.CMSRuntimeException;
import com.wootide.cms.common.ErrorCodes;
import com.wootide.cms.common.Page;
import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.common.dao.CommonDao;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.consumables.supplier.dao.SupplierInfoDao;
import com.wootide.cms.service.consumables.supplier.pojo.SupplierInfo;
import com.wootide.cms.service.consumables.supplier.transaction.SupplierInfoTransaction;

/**
 * 供应商事务层实现
 * 〈功能详细描述〉
 * @author wuliquan
 * @version 2013-2-25
 * @see SupplierInfoTransactionImpl
 * @since
 */
public class SupplierInfoTransactionImpl implements SupplierInfoTransaction
{
    /**
     * 注入运行日志
     */
    private RunLog runLog;
    
    /**
     * 公共数据访问层实例
     */
    private CommonDao commonDao;
    
    /**
     * 供应商数据访问层实例
     */
    private SupplierInfoDao supplierInfoDao;
    

    public void addOneSupplier(SupplierInfo supplierInfo)
    {
        // 打印方法入口参数
        runLog.debug("supplierInfo=" + supplierInfo);
        // 根据供应商检查唯一性
        Integer num = supplierInfoDao.findNumBySupplierName(supplierInfo.getSupplierName());
        // 供应商单位记录已经存在
        if (num > 0)
        {
            throw new CMSRuntimeException("SupplierInfo Existed!",
                    ErrorCodes.Consumables.Supplier.SUPPLIER_EXIST);
        }
        
        commonDao.addOneObject(supplierInfo);
    }

    public SupplierInfo delOneSupplier(Integer supplierId)
    {
        SupplierInfo oldSupplier = supplierInfoDao.findOneSupplierBySupplierId(supplierId);
        //判断要删除的记录是否存在
        if (null == oldSupplier)
        {
            throw new CMSRuntimeException("Supplier not Exist!",
                    ErrorCodes.Consumables.Supplier.SUPPLIER_NOTEXIST);
        }
        
        commonDao.delOneObject(oldSupplier);
        //        BeanUtils.copyProperties(oldSupplier, oldSupplierCopy);
        return oldSupplier;
    }

    public SupplierInfo modOneSupplier(SupplierInfo supplierInfo)
    {
        // 打印方法入口参数
        runLog.debug("supplierInfo=" + supplierInfo);
        SupplierInfo oldSupplier = supplierInfoDao.findOneSupplierBySupplierId(supplierInfo.getSupplierId());
        
        //判断要修改的记录是否存在
        if (null == oldSupplier)
        {
            throw new CMSRuntimeException("Supplier not Exist!",
                    ErrorCodes.Consumables.Supplier.SUPPLIER_EXIST);
        }
        
        //获取原始数据副本
        SupplierInfo oldSupplierCopy = new SupplierInfo();
        BeanUtils.copyProperties(oldSupplier, oldSupplierCopy);
        
        //修改供应商信息
        oldSupplier.setSupplierName(supplierInfo.getSupplierName());
        oldSupplier.setSupplierAddr(supplierInfo.getSupplierAddr());
        oldSupplier.setSupplierTel(supplierInfo.getSupplierTel());
        oldSupplier.setSupplierEmail(supplierInfo.getSupplierEmail());
        
        oldSupplier.setContactName(supplierInfo.getContactName());
        oldSupplier.setContactTel(supplierInfo.getContactTel());
        
        return oldSupplierCopy;
    }

    public QueryResults queryForPages(Page page, QueryCondition queryCondition)
    {
        // 打印方法入口参数
        runLog.debug("page=" + page);
        runLog.debug("queryCondition=" + queryCondition);
        QueryResults queryResults = commonDao.queryForPages(page, queryCondition);
        return queryResults;
    }

    public SupplierInfo viewOneSupplier(Integer supplierId)
    {
        // 打印方法入口参数
        runLog.debug("innerIndex=" + supplierId);
        SupplierInfo supplier = supplierInfoDao.findOneSupplierBySupplierId(supplierId);
        return supplier;
    }

    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }

    public void setCommonDao(CommonDao commonDao)
    {
        this.commonDao = commonDao;
    }

    public void setSupplierInfoDao(SupplierInfoDao supplierInfoDao)
    {
        this.supplierInfoDao = supplierInfoDao;
    }

}
